package com.jungle.client;

import com.jungle.bfi.domain.vo.DatasetItemVo;
import com.jungle.client.control.BasicDialog;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

import java.io.IOException;
import java.net.URL;
import java.util.function.Consumer;

import static com.jungle.client.FxApplication.bundle;

public class DatasetItemEditController extends BasicDialog<DatasetItemVo> {

    @FXML
    private TextField displayValueField;
    @FXML
    private TextField valueField;

    @Override
    public void loadData(DatasetItemVo data) {
        this.displayValueField.setText(data.getDisplayValue());
        this.valueField.setText(data.getValue());
        this.id = data.getId();
    }

    private String id;
    @Override
    public DatasetItemVo storeData() {
        DatasetItemVo data = new DatasetItemVo();
        data.setId(this.id);
        data.setDisplayValue(this.displayValueField.getText());
        data.setValue(this.valueField.getText());
        return data;
    }

    private static final URL FXML_URL = DatasetItemEditController.class.getResource("datasetItem-edit.fxml");
    private static final String TITLE = bundle.getString("datasetItem.dialog.title");

    public static void showAddDialog(DatasetItemVo dataSetVo, Consumer<DatasetItemVo> consumer) throws IOException {

        String title = bundle.getString("dialog.title.prefix.add") + TITLE;

        new DatasetItemEditController().showDialog(FXML_URL, title, dataSetVo, consumer);
    }

    public static void showEditDialog(DatasetItemVo dataSetVo, Consumer<DatasetItemVo> consumer) throws IOException {

        String title = bundle.getString("dialog.title.prefix.edit") + TITLE;

        new DatasetItemEditController().showDialog(FXML_URL, title, dataSetVo, consumer);
    }
}
